package repository;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import domain.PropertyScrap;


public class PropertyScrapDAO extends BaseDAO {
	private static final long serialVersionUID = 2634298180683392440L;

	public PropertyScrapDAO() throws SQLException { super();	}
	@Override
	public ResultSet query(Object... params) {
		String sql = "SELECT * FROM 资产报废表 WHERE 物品ID=?;";
		ResultSet rs = null;
		try {
			PreparedStatement pst = con.prepareStatement(sql);
			pst.setString(1, (String)params[0]);
			rs = pst.executeQuery();
			
		}catch(SQLException e){
			e.printStackTrace();
		}
		return rs;
	}
	
	public ResultSet queryAll() {
		String sql = "SELECT * FROM 资产报废表;";
		ResultSet rs = null;
		
		try {
			PreparedStatement pst = con.prepareStatement(sql);			
			rs = pst.executeQuery();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return rs;
	}

	@Override
	public int update(Object... params) {
		String sql = "UPDATE 资产报废表 SET 物品名称=?,物品源属地=?,数量=? WHERE 物品ID=?;";
		int num = 0;
		try {
			PreparedStatement pst = con.prepareStatement(sql);
			PropertyScrap prosc = (PropertyScrap)params[0];
			pst.setString(1, prosc.getName());
			pst.setString(2, prosc.getLocation());
			pst.setString(3, prosc.getNumber());
			pst.setString(4, prosc.getID());
			num = pst.executeUpdate();
		}catch(SQLException e) {
			e.printStackTrace();
		}
		return num;
	}

	@Override
	public int delete(Object... params) {
		String sql = "DELETE FROM 资产报废表 WHERE 物品ID=?;";
		int num = 0;
		try {
			PreparedStatement pst = con.prepareStatement(sql);
			pst.setString(1, (String)params[0]);
			num = pst.executeUpdate();
		}catch(SQLException e) {
			e.printStackTrace();
		}
		return num;
	}

	@Override
	public int insert(Object... params) {
		String sql = "INSERT INTO 资产报废表(物品ID,物品名称,物品源属地,数量)"+"VALUES(?,?,?,?);";
		int num = 0;
		try {
			PreparedStatement pst = con.prepareStatement(sql);
			PropertyScrap prosc = (PropertyScrap)params[0];
			pst.setString(1, prosc.getID());
			pst.setString(2, prosc.getName());
			pst.setString(3, prosc.getLocation());
			pst.setString(4, prosc.getNumber());
			num = pst.executeUpdate();
		}catch(SQLException e) {
			e.printStackTrace();
		}
		return num;
	}

}
